package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lepu.app.fun.my.MyUserInfoIllHistoryActivity;
import com.lepu.app.usercenter.bean.LoginInfoIllnessTags;
import com.lepu.pasm.R;

public class AdapterMyUserInfoIllHistory extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private MyUserInfoIllHistoryActivity mContext = null;
	private ArrayList<LoginInfoIllnessTags> mArrayList = null;
	private ListView mListView = null;

	@SuppressLint("UseSparseArrays")
	private HashMap<Integer, Object> mMaps = new HashMap<Integer, Object>();
	
	public AdapterMyUserInfoIllHistory(MyUserInfoIllHistoryActivity context,ArrayList<LoginInfoIllnessTags> arrayList,ListView listView) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mListView = listView;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_userinfo_ill_history_activity_listitem, null);

			holder = new ViewHolder();

			RelativeLayout mainItemLayout = (RelativeLayout) convertView
					.findViewById(R.id.mainItemLayout);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);

			holder.mainItemLayout = mainItemLayout;
			holder.titleTextView = titleTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final LoginInfoIllnessTags item = mArrayList.get(position);
		holder.titleTextView.setText(item.TagTitle);
		
		if (mMaps.containsKey(position)) {
			holder.mainItemLayout.setSelected(true);
		} else {
			holder.mainItemLayout.setSelected(false);
		}
		
		holder.mainItemLayout.setTag(String.format("layout_%d", position));
		
		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				dowithIllHistory(item,position,holder.mainItemLayout);
			}
		});
		
		return convertView;
	}
	
	private void dowithIllHistory(LoginInfoIllnessTags item,int position,RelativeLayout mainItemLayout)
	{
		int endIndex = mArrayList.size() - 1;
		if(position != endIndex)
		{
			if(mMaps.containsKey(endIndex)){
				mMaps.remove(endIndex);
				
				RelativeLayout layout = (RelativeLayout) mListView.findViewWithTag(String.format("layout_%d", endIndex));
				if(layout != null)
				{
					layout.setSelected(false);
				}
			}
			
			if(!mainItemLayout.isSelected()){
				mContext.mSelectedList.add(item);
				mMaps.put(position, "");
			}else{
				mContext.mSelectedList.remove(item);
				mMaps.remove(position);
			}
			
			mainItemLayout.setSelected(!mainItemLayout.isSelected());
			
		}else{
			//无病史
			if(!mainItemLayout.isSelected()){
				
				mMaps.clear();
				mContext.mSelectedList.clear();
				
				mMaps.put(position, "");
				//mContext.mSelectedList.add(item);
				
				mainItemLayout.setSelected(!mainItemLayout.isSelected());
				
				notifyDataSetChanged();
			}else{
				mMaps.remove(position);
				
				mainItemLayout.setSelected(!mainItemLayout.isSelected());
			}
		}
		
	}

	private static class ViewHolder {
		RelativeLayout mainItemLayout;
		TextView titleTextView;
	}
}
